﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using Infrastructure;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Areas.HisApi.Controllers
{
    /// <summary>
    /// 账单查询
    /// </summary>
    [Route("HisApi/[controller]/[action]")]
    [ApiController]
    public class BillController : ControllerBase
    {
        /// <summary>
        /// 查询HIS账单信息（账单查询）
        /// </summary>
        private readonly CGGYW006App cggyw006App;
        /// <summary>
        /// 一日清
        /// </summary>
        private readonly usp_bqmrmx_hfAPP usp_bqmrmx_hfAPP;
        HsLogApp hsApp;
        public BillController(CGGYW006App _cggyw006App, usp_bqmrmx_hfAPP _usp_bqmrmx_hfAPP, HsLogApp _hsApp)
        {
            cggyw006App = _cggyw006App;
            usp_bqmrmx_hfAPP = _usp_bqmrmx_hfAPP;
            hsApp = _hsApp;


        }
        /// <summary>
        /// 查询HIS账单信息（账单查询）///测试没问题
        /// </summary>
        /// <param name="cxrq">查询日期</param>
        /// <param name="zflb">支付类别</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCGGYW006ByCxrq(string cxrq, string zflb)
        {
            //准备返回对象
            var result = new Response<List<CGGYW006>>();
            var queryResult = cggyw006App.QueryCGGYW006ByCxrq(cxrq, zflb);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return (new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    hiszdxxs = result.Result.First()
                }).ToJson();
            }
            else
            {
                return (new
                {
                    success = "false",
                    requestId = Guid.NewGuid().ToString(),
                    message = result.Message
                }).ToJson();
            }
        }
        /// <summary>
        /// 一日清
        /// </summary>
        /// <param name="zyh"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        [HttpGet]
        public string QueryYRQ(string zyh, string time)
        {
            var result = new Response<List<usp_bqmrmx_hf>>();
            var queryRresult = usp_bqmrmx_hfAPP.QueryYRQ(zyh, time);
            result.Result = queryRresult.ToList();
            if (result.Result.Count > 0)
            {
                return (new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    yrq = result.Result
                }).ToJson();
            }
            else
            {
                return (new
                {
                    success = "false",
                    requestId = Guid.NewGuid().ToString(),
                    message = "没有查询到数据"
                }).ToJson();
            }
        }
        /// <summary>
        /// 微信公众号使用 身份证和姓名
        /// </summary>
        /// <param name="zjh"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public string QueryXG(string zjh, string name)
        {
            var result = new Response<List<Xgbb>>();
            var queryRresult = usp_bqmrmx_hfAPP.QueryXG(zjh, name);
            result.Result = queryRresult.ToList();
            if (result.Result.Count > 0)
            {
                return (new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    yrq = result.Result
                }).ToJson();
            }
            else
            {
                return (new
                {
                    success = "false",
                    requestId = Guid.NewGuid().ToString(),
                    message = "没有查询到数据"
                }).ToJson();
            }
        }
        /// <summary>
        /// 打印核酸插入记录
        /// </summary>
        /// <param name="djh"></param>
        /// <returns></returns>
        [HttpGet]
        public string InsertLog(string djh)
        {
            hsApp.InsertLog(Int32.Parse( djh));
             return (new
            {
                success = "true",
                requestId = Guid.NewGuid().ToString(),
                
            }).ToJson();
        }
        [HttpGet]
        public string QueryXGZ(string zjh)
        {
            var result = new Response<List<Xgbb>>();
            var queryRresult = usp_bqmrmx_hfAPP.QueryXGZ(zjh);
            result.Result = queryRresult.ToList();
            if (result.Result.Count > 0)
            {
                var temp = hsApp.QueryByDJH( queryRresult.FirstOrDefault().djh);
                if (temp != null)
                {
                    return (new
                    {
                        
                        message = "已经打印过报告，如需打印请联系检验科"
                    }).ToJson();
                }
                else
                {


                    return (new
                    {
                        success = "true",
                        requestId = Guid.NewGuid().ToString(),
                        yrq = result.Result
                    }).ToJson();

                }
            }
            else
            {
                return (new
                {
                    success = "false",
                    requestId = Guid.NewGuid().ToString(),
                    message = "没有查询到数据"
                }).ToJson();
            }
            //result.Result = queryRresult.ToList();
            //if (result.Result.Count > 0)
            //{
            //    return (new
            //    {
            //        success = "true",
            //        requestId = Guid.NewGuid().ToString(),
            //        yrq = result.Result
            //    }).ToJson();
            //}
            //else
            //{
            //    return (new
            //    {
            //        success = "false",
            //        requestId = Guid.NewGuid().ToString(),
            //        message = "没有查询到数据"
            //    }).ToJson();
            //}
        }

    }
}